package com.sdkx.app.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.sdkx.app.R;
import com.sdkx.app.ViewModel.AirWayManager;
import com.sdkx.app.ViewModel.CurAirWay;
import com.sdkx.app.activitys.MainActivity;
import com.sdkx.app.adapter.RoutesListAdapter;
import com.sdkx.app.datalayers.AirWayLine;
import com.sdkx.app.datalayers.LayersFactory;
import com.sdkx.app.model.AirWay;
import com.sdkx.app.model.WayPoint;

import java.util.List;

public class RoutesFragmentView2 extends BaseFragment implements View.OnClickListener {
    //航线管理二级目录
    private ListView lvGroup;
    private RoutesListAdapter routedapter;
    InitTask initTask;
    private EditText searchEdittext;
    PopupWindow popwindow;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return initView(inflater, container);
    }

    private View initView(LayoutInflater inflater, final ViewGroup container) {
        //航线列表
        View rootView = inflater.inflate(R.layout.fragment_route_list, container, false);
        rootView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        searchEdittext = rootView.findViewById(R.id.seach_edit);
        rootView.findViewById(R.id.add).setOnClickListener(this);
        rootView.findViewById(R.id.importfile).setOnClickListener(this);
        lvGroup = (ListView) rootView.findViewById(R.id.lvGroup);
        TextView text_tip = (TextView) rootView.findViewById(R.id.text_tip);
        //当三级菜单没有数据的时候提醒的样式
        lvGroup.setEmptyView(text_tip);
        routedapter = new RoutesListAdapter(getActivity());
        routedapter.setList(AirWayManager.getItems(null));
        lvGroup.setAdapter(routedapter);
        //返回上一级点击事件
        Button btn_back = (Button) rootView.findViewById(R.id.btn_back);
        //返回上一级按钮
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goBack();
            }
        });
        searchEdittext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String info = s.toString();
                routedapter.setList(AirWayManager.getItems(info));
//                if(!TextUtils.isEmpty(info)) {
//                    AirWayManager.getItems(info);
//                }else{
//                    AirWayManager.getItems(info);
//                }

            }
        });
        //点击
        lvGroup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                View contentView = getActivity().getLayoutInflater().inflate(
                        R.layout.pop_four_routes, null);
                contentView.findViewById(R.id.tv1).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AirWay way = (AirWay) routedapter.getItem(position);
                        List<WayPoint> wayPoints = AirWayManager.getWayPointsInAirWay(way.getName());
                        CurAirWay.getInstance().setList(wayPoints);
                        CurAirWay.getInstance().setName(way.getName());
                        AirWayLine line = (AirWayLine) LayersFactory.getInstance().getItemByClass(AirWayLine.class);
                        line.setWayPoints(AirWayManager.getWayPointsInAirWay( way.getName()));
                        ((MainActivity) getActivity()).getMapControl().addCustomDraw(line);
                        ((MainActivity) getActivity()).getMapControl().refresh();
                        popwindow.dismiss();

                    }
                });
                View modify = contentView.findViewById(R.id.modify);
                modify.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AirWay way = (AirWay) routedapter.getItem(position);
                        Bundle bundle = new Bundle();
                        bundle.putString("mode", "edit");
                        bundle.putString("AIRWAY_NAME", way.getName());
                        startFragment(RouteEditFragment.class, bundle);
                        popwindow.dismiss();
                    }
                });
                contentView.findViewById(R.id.tv3).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AirWay way = (AirWay) routedapter.getItem(position);
                        Bundle bundle = new Bundle();
                        bundle.putString("AIRWAY_NAME", way.getName());
                        startFragment(AirWayDetailFragment.class, bundle);
                        popwindow.dismiss();
                    }
                });

                contentView.findViewById(R.id.tv4).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AirWay way = (AirWay) routedapter.getItem(position);
                        String copyname = way.getName() + "copy";
                        if (AirWayManager.cotains(copyname)) {
                            Toast.makeText(getContext(), "副本已经存在", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        List<WayPoint> wayPoints = AirWayManager.getWayPointsInAirWay(way.getName());
                        AirWay copyway = new AirWay();
                        copyway.setName(way.getName() + "copy");
                        copyway.setIsShow(way.getIsShow());
                        copyway.setIsEdit(way.getIsEdit());
                        copyway.setType(way.getType());
                        copyway.save();
                        AirWayManager.updateWayPointsInAirWay(copyway.getName(), wayPoints);
                        routedapter.setList(AirWayManager.getItems(null));
                    }
                });
                contentView.findViewById(R.id.tv5).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AirWay way = (AirWay) routedapter.getItem(position);
                        AirWayManager.delete(way.getName());
                        routedapter.setList(AirWayManager.getItems(""));
                        routedapter.notifyDataSetChanged();
                        popwindow.dismiss();
                    }
                });
                AirWay airWay = (AirWay) parent.getAdapter().getItem(position);
                if (airWay.getIsEdit()) {
                    modify.setVisibility(View.VISIBLE);
                } else {
                    modify.setVisibility(View.GONE);
                }
                popwindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                popwindow.setBackgroundDrawable(new ColorDrawable(
                        Color.TRANSPARENT));
                popwindow.setOutsideTouchable(true);
                popwindow.setFocusable(true);
                popwindow.showAsDropDown(view, view.getWidth() - 30, -view.getHeight(), Gravity.LEFT | Gravity.TOP);

            }
        });
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initTask = new InitTask();
        initTask.execute();
    }

    @Override
    public void onDestroyView() {
        initTask.cancel(true);
        super.onDestroyView();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add:
                Bundle bundle = new Bundle();
                bundle.putString("mode", "add");
                startFragment(RouteEditFragment.class, bundle);
                break;
            case R.id.importfile:
                break;
        }
    }

    private class InitTask extends AsyncTask<Void, Void, List<AirWay>> {

        @Override
        protected List<AirWay> doInBackground(Void... voids) {
            return AirWayManager.getItems("");
        }

        @Override
        protected void onPostExecute(List<AirWay> airWays) {
            if (routedapter != null)
                routedapter.setList(airWays);
        }
    }

}
